/*****************************************************************
* Do File: reg_gini_dissim.do
*
* Purpose:
*   This do file takes as input the metro_level data and processes it to
*   analyze changes in variables related to inequality and dissimilarity
*   over time.
*
* Process:
*   - Select data for the years 1980 and 2010.
*   - Calculate the levels of key variables for 1980.
*   - Compute the differences in these variables between 1980 and 2010.
*   - Generate regression tables (Tables 11 and 12) and summary tables (Tables 12 and 14).
*
*****************************************************************/








use "$output/metro_level.dta", clear
keep if year ==1980|year==2010


sort metro year

forvalues i = 1/11 {
	by metro: gen diff_emp_share`i' = emp_share`i'[_n+1] - emp_share`i'[_n]
}
by metro: gen diff_white =	white[_n+1] - white[_n]
by metro: gen diff_black = 	black[_n+1] - black[_n]
by metro: gen diff_indian =indian[_n+1] -indian[_n]
by metro: gen diff_asian = 	asian[_n+1] - asian[_n]
by metro: gen diff_other = 	other[_n+1] - other[_n]
by metro: gen diff_famkids =   famkidsdissim[_n+1] -   	famkidsdissim[_n]
by metro: gen diff_famkidsgini = famkidsgini[_n+1] -      famkidsgini[_n]
by metro: gen diff_allfam = 	allfamdissim[_n+1] -   	 allfamdissim[_n]
by metro: gen diff_allfamgini =   allfamgini[_n+1] - 	   allfamgini[_n]

keep if year==1980

rename allfamdissim diss_all_fam_80
rename allfamgini gini_all_fam_80
rename diff_allfam diss_all_fam_8010
rename diff_allfamgini gini_all_fam_8010
rename famkidsdissim diss_fam_kids_80
rename famkidsgini gini_fam_kids_80
rename diff_famkids diss_fam_kids_8010
rename diff_famkidsgini gini_fam_kids_8010
rename population population80

foreach i in 1 2 3 4 5 6 7 8 9 10 11 {
	rename emp_share`i' emp_share_80_`i' 
	rename diff_emp_share`i' emp_share_8010_`i' 
}

rename white white80
rename black black80
rename indian indian80
rename asian asian80
rename other other80
rename diff_white white8010
rename diff_black black8010
rename diff_indian indian8010
rename diff_asian asian8010
rename diff_other other8010
keep if year ==1980
drop year

tempfile metro_level 
save `metro_level', replace 














*****************************
** Run all the regressions 
*****************************

** Table 11: Levels 

use `metro_level', clear
merge 1:1 metro using "$path/Raw_Data/saiz_elas.dta"
drop _merge

label variable elasticity "Housing Elasticity"


** Generate the corss term 
gen elas_dissim = elasticity*diss_all_fam_80


** 3. 2010 - 1980 changes (Weighted regression)

est clear

** Baseline regression: delta dissim on delta gini 
reg gini_all_fam_80 diss_all_fam_80 [aweight = population80]
est sto r1 


** Race controls 
reg gini_all_fam_80 diss_all_fam_80 white80 black80 asian80 indian80 other80 [aweight=population80]
est sto r2 


*** Industry Controls
reg gini_all_fam_80 diss_all_fam_80 emp_share_80_1-emp_share_80_10 [aweight=population80]
est sto r3 


*** Race + Industry 
reg gini_all_fam_80 diss_all_fam_80 white80 black80 asian80 indian80 other80 emp_share_80_1-emp_share_80_10  [aweight=population]
est sto r4


** Race + Industry + Housing Elasticity 
reg gini_all_fam_80 diss_all_fam_80 white80 black80 asian80 indian80 other80 emp_share_80_1-emp_share_80_10 elasticity [aweight=population]
est sto r5 


esttab r1 r2 r3 r4 r5  using "$output/reg_dissim_gini_levels.csv", replace nogaps  ///
keep(diss_all_fam_80  _cons) ///
coeflabel(diss_all_fam_80 "\$Dissimilarity\$"  _cons "Constant") ///
b(3) se(3) stats(N r2 r2_a, labels("N" "R2" "Adjusted R2") fmt(%9.0fc %9.3f)) star(* 0.10 ** 0.05 *** 0.01)  ///
mtitle("\$Gini\$" "\$Gini\$" "\$Gini\$" "\$Gini\$" "\$Gini\$") ///
title(Gini and dissimilarity (1980) )




** Table 12: Changes 

// Table 12 changes //-- 

use `metro_level', clear
merge 1:1 metro using "$path/Raw_Data/saiz_elas.dta"
drop _merge

label variable elasticity "Housing Elasticity"


** Generate the corss term 
gen elas_dissim = elasticity*diss_all_fam_8010


** 3. 2010 - 1980 changes (Weighted regression)

est clear

** Baseline regression: delta dissim on delta gini 
reg gini_all_fam_8010 diss_all_fam_8010 [aweight = population80]
est sto r1 


** Race controls 
reg gini_all_fam_8010 diss_all_fam_8010 white8010 black8010 asian8010 indian8010 other8010 [aweight=population80]
est sto r2 


*** Industry Controls
reg gini_all_fam_8010 diss_all_fam_8010 emp_share_8010_1-emp_share_8010_10 [aweight=population80]
est sto r3 


*** Race + Industry 
reg gini_all_fam_8010 diss_all_fam_8010 white8010 black8010 asian8010 indian8010 other8010 emp_share_8010_1-emp_share_8010_10 [aweight=population]
est sto r4


** Race + Industry + Housing Elasticity 
reg gini_all_fam_8010 diss_all_fam_8010 white8010 black8010 asian8010 indian8010 other8010 emp_share_8010_1-emp_share_8010_10  elasticity [aweight=population]
est sto r5 



*** Export the regression results 
esttab r1 r2 r3 r4 r5 using "$output/reg_dissim_gini_changes.csv", replace ///
keep(diss_all_fam_8010 _cons) ///
coeflabel(diss_all_fam_8010 "$\Delta Dissimilarity$" _cons "Constant") ///
b(3) se(3)  stats(N r2 r2_a, labels("N" "R2" "Adjusted R2") fmt(%9.0fc %9.3f)) star(* 0.10 ** 0.05 *** 0.01)  ///
mtitle("$\Delta Gini$" "$\Delta Gini$" "$\Delta Gini$" "$\Delta Gini$" "$\Delta Gini$") ///
indicate("$\Delta Race$ = white8010 black8010 asian8010 indian8010 other8010" "$\Delta Industry$ = emp_share_8010_*" "Housing Elasticity = elasticity") ///
title(Change in gini and dissimilarity (1980-2010) )




** Table 13: Summary Levels 

// Table 13: Summary of levels 



use `metro_level', clear
merge 1:1 metro using "$path/Raw_Data/saiz_elas.dta"
drop _merge

label variable elasticity "Housing Elasticity"


** Generate the corss term 
gen elas_dissim = elasticity*diss_all_fam_80


** 3. 2010 - 1980 changes (Weighted regression)

est clear

* 1. Post the summary statistics with weights
* 1. Post the summary statistics with weights
estpost summarize diss_all_fam_80 gini_all_fam_80 white80 black80 asian80 indian80 other80 ///
    emp_share_80_1-emp_share_80_11 elasticity  [aweight=population80], detail
estimates store level_sum_stats

*esttab level_sum_stats using "$path/tables/wt_summary_levels.txt", replace noobs

* 3. Export to .tex with custom labels and formatting
* Use single backslashes for LaTeX commands
esttab level_sum_stats using "$output/wt_summary_levels.csv", replace noobs ///
    cells("mean(fmt(%9.2f)) min(fmt(%9.2f)) max(fmt(%9.2f)) sd(fmt(%9.2f))") ///
    label nonumbers ///
    varlabels(diss_all_fam_80 "$\text{Dissimilarity}_{1980}$" ///
              gini_all_fam_80 "$\text{Gini}_{1980}$" ///
              white80 "$\text{Pct. White}_{1980}$" ///
              black80 "$\text{Pct. Black}_{1980}$" ///
              indian80 "$\text{Pct. Indian}_{1980}$" ///
              asian80 "$\text{Pct. Asian}_{1980}$" ///
              other80 "$\text{Pct. Other}_{1980}$" ///
              emp_share_80_1 "$\text{Pct. Agri.}_{1980}$" ///
              emp_share_80_2 "$\text{Pct. Construction}_{1980}$" ///
              emp_share_80_3 "$\text{Pct. FIRE}_{1980}$" ///
              emp_share_80_4 "$\text{Pct. Manufacturing}_{1980}$" ///
              emp_share_80_5 "$\text{Pct. Mining}_{1980}$" ///
              emp_share_80_6 "$\text{Pct. Nonclassifiable Establishments}_{1980}$" ///
              emp_share_80_7 "$\text{Pct. Public Administration}_{1980}$" ///
              emp_share_80_8 "$\text{Pct. Retail Trade}_{1980}$" ///
              emp_share_80_9 "$\text{Pct. Services}_{1980}$" ///
              emp_share_80_10 "$\text{Pct. Trasnsportation and Public Utilities}_{1980}$" ///
              emp_share_80_11 "$\text{Pct. Wholesale Trade}_{1980}$" ///
              elasticity "Housing Elasticity" ) 




** Table 14: Summary Changes 
 
use `metro_level', clear
merge 1:1 metro using "$path/Raw_Data/saiz_elas.dta"
drop _merge

label variable elasticity "Housing Elasticity"


** Generate the corss term 
gen elas_dissim = elasticity*diss_all_fam_8010



est clear

* 1. Post the summary statistics with weights for the 2010-1980 changes
estpost summarize diss_all_fam_8010 gini_all_fam_8010 white8010 black8010 asian8010 indian8010 other8010 ///
    emp_share_8010_1 emp_share_8010_2 emp_share_8010_3 emp_share_8010_4 emp_share_8010_5 emp_share_8010_6 ///
    emp_share_8010_7 emp_share_8010_8 emp_share_8010_9 emp_share_8010_10 [aweight=population80], detail

* 2. Store the results
estimates store summ_stats_2010

* 3. Export to .tex with custom labels and formatting
* Single backslashes for LaTeX, and wrap in math mode where needed
esttab summ_stats_2010 using "$output/wt_summary_changes_2010.csv", replace noobs ///
    cells("mean(fmt(%9.2f)) min(fmt(%9.2f)) max(fmt(%9.2f)) sd(fmt(%9.2f))") ///
    label nonumbers ///
    varlabels(diss_all_fam_8010 "$\Delta \text{Dissimilarity}_{2010-1980}$" ///
              gini_all_fam_8010 "$\Delta \text{Gini}_{2010-1980}$" ///
              white8010 "$\Delta \text{Pct. White}_{2010-1980}$" ///
              black8010 "$\Delta \text{Pct. Black}_{2010-1980}$" ///
              indian8010 "$\Delta \text{Pct. Indian}_{2010-1980}$" ///
              asian8010 "$\Delta \text{Pct. Asian}_{2010-1980}$" ///
              other8010 "$\Delta \text{Pct. Other}_{2010-1980}$" ///
              emp_share_8010_1 "$\Delta \text{Pct. Agri.}_{2010-1980}$" ///
              emp_share_8010_2 "$\Delta \text{Pct. Construction}_{2010-1980}$" ///
              emp_share_8010_3 "$\Delta \text{Pct. FIRE}_{2010-1980}$" ///
              emp_share_8010_4 "$\Delta \text{Pct. Manufacturing}_{2010-1980}$" ///
              emp_share_8010_5 "$\Delta \text{Pct. Mining}_{2010-1980}$" ///
              emp_share_8010_6 "$\Delta \text{Pct. Nonclassifiable Establishments}_{2010-1980}$" ///
              emp_share_8010_7 "$\Delta \text{Pct. Public Administration}_{2010-1980}$" ///
              emp_share_8010_8 "$\Delta \text{Pct. Retail Trade}_{2010-1980}$" ///
              emp_share_8010_9 "$\Delta \text{Pct. Services}_{2010-1980}$" ///
              emp_share_8010_10 "$\Delta \text{Pct. Trasnsportation and Public Utilities}_{2010-1980}$" )















